package monitor

import (
	"net/http"
	"github.com/go-kratos/kratos/v2/middleware"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"go.opentelemetry.io/otel"
)

/*
 * @Author: chengjiang
 * @Date: 2025-07-26 00:10:29
 * @Description: 监控与日志
 */

// 链路追踪
func TracingMiddleware() middleware.Middleware {
  return tracing.Server(
    tracing.WithTracerProvider(otel.GetTracerProvider()),
  )
}

// Prometheus指标
func NewMetrics(serviceName string) http.Handler {
  reg := prometheus.NewRegistry()
  reg.MustRegister(promauto.NewCounterVec(
    prometheus.CounterOpts{
      Name: "http_requests_total",
      Help: "Total HTTP requests",
    },
    []string{"service", "path", "code"},
  ))
  return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
}